﻿using AntdUI;
using Lima.ResumeSys.DAL;
using Lima.ResumeSys.Models;
using Lima.ResumeSys.Util;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Lima.ResumeSys.Models.RewardPunish;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Lima.ResumeSys.PersonControl
{

    public partial class EditRewardPunish : UserControl
    {
        private AntdUI.Window window;
        private string _PerNo = "";
        private bool result = false;
        List<RewardPunish> rewList;
        private string _RewId = "";
        RewardPunishDAL rewDal = new RewardPunishDAL();
        public EditRewardPunish(AntdUI.Window _window, string perNo)
        {
            window = _window;
            _PerNo = perNo;
            InitializeComponent();
            InitRewardPunishColumns();
            InitRewardPunishData();
        }


        #region 廉政人员奖惩信息


        private void tabRewardPunish_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            switch (e.Btn.Text)
            {
                //暂不支持进入整行编辑，只支持指定单元格编辑，推荐使用弹窗或抽屉编辑整行数据
                case "编辑":
                    RewardPunish entity = rewDal.GetModel(e.Btn.Id);
                    txtRewDate.Value = entity.RewDate;
                    txtRewRecord.Text = entity.RewRecord;
                    txtRewTitle.Text = entity.RewTitle;
                    _RewId = entity.RewId;
                    break;
                case "删除":
                    var result = Modal.open(window, "删除警告！", "确认要删除选择的数据吗？", TType.Warn);
                    if (result == DialogResult.OK)
                    {
                        if (rewDal.Del(e.Btn.Id))
                        {
                            AntdUI.Message.success(window, "删除成功！", autoClose: 3);
                        }
                        else
                        {
                            AntdUI.Message.error(window, "删除失败！", autoClose: 3);
                        }
                    }

                    break;
            }
            InitRewardPunishData();
        }
        private void InitRewardPunishColumns()
        {
            tabRewardPunish.Columns = new ColumnCollection() {
                new Column("","序号"){
                    Width = "50",
                    Render = (value,record,rowindex)=>{return (rowindex+1); },
                    Fixed = true,//冻结列
                },
                new Column("RewDateView", "奖惩时间", ColumnAlign.Center),
                new Column("RewTitle", "奖惩记录标题", ColumnAlign.Left),
                new Column("RewRecord", "奖惩记录",ColumnAlign.Left),
                new Column("UpdatedBy", "操作人",ColumnAlign.Left),
                new Column("CellLinks", "操作", ColumnAlign.Center),
            };
        }
        private void InitRewardPunishData()
        {
            rewList = new List<RewardPunish>();

            QueryRewardPunishModel query = new QueryRewardPunishModel();
            query.PerNo = _PerNo;
            var list = rewDal.GetList(query);
            foreach (var item in list)
            {
                rewList.Add(new RewardPunish
                {
                    RewDateView = item.RewDate.ToString("yyyy-MM-dd"),
                    RewTitle = item.RewTitle,
                    RewRecord = item.RewRecord,
                    PerNo = item.PerNo,
                    UpdatedBy = item.UpdatedBy,
                    CellLinks = new CellLink[] {
                    new CellButton(item.RewId,"编辑",TTypeMini.Primary),
                    new CellButton(item.RewId,"删除",TTypeMini.Error)},
                });
            }
            tabRewardPunish.DataSource = rewList;
        }

        /// <summary>
        /// 廉政人员奖惩信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRPSave_Click(object sender, EventArgs e)
        {
            result = false;
            if (txtRewDate.Text == "" || txtRewTitle.Text == "" || txtRewRecord.Text == "")
            {
                MessageBox.Show("奖惩信息不完整,无法添加！", "提示", MessageBoxButtons.OK);
                return;
            }
            RewardPunish entity = new RewardPunish();
            entity.RewDate = DateTime.ParseExact(txtRewDate.Text, "yyyy-MM-dd", CultureInfo.InvariantCulture);
            entity.RewTitle = txtRewTitle.Text;
            entity.RewRecord = txtRewRecord.Text;
            entity.PerNo = _PerNo;

            if (string.IsNullOrWhiteSpace(_RewId))
            {
                _RewId = Guid.NewGuid().ToString();
                entity.RewId = _RewId;
                entity.Creator = "";
                entity.UpdatedBy = "";
                result = rewDal.Add(entity);
            }
            else
            {
                entity.RewId = _RewId;
                entity.UpdatedBy = "";
                result = rewDal.Update(entity);
            }
            if (result)
            {
                AntdUI.Message.success(window, "操作成功！", autoClose: 3);
                txtRewDate.Value = null;
                txtRewTitle.Text = "";
                txtRewRecord.Text = "";
                InitRewardPunishData();
            }
        }




        #endregion
    }
}
